//
//  FeedbackVC.m
//  ProjectSource_Demo
//
//  Created by Liu on 16/3/24.
//  Copyright © 2016年 AngryBear. All rights reserved.
//

#import "FeedbackVC.h"

#define PLACEHOLDER_STRING @"请输入反馈意见"
#define PLACEHOLDER_COLOR [UIColor colorWithHexRGB:0xb0b0b0]

@interface FeedbackVC ()<UITextViewDelegate>
{
    CGFloat _originalY;
}
@property (nonatomic, strong) UITextView *textView;
@property (nonatomic, strong) UIButton *commitButton;

@end

@implementation FeedbackVC

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.view.backgroundColor = [UIColor colorWithHexRGB:0xefefef];
    self.title = @"意见与反馈";

    [self.view addSubview:self.textView];
    [self.view addSubview:self.commitButton];
    
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.mas_equalTo(@0);
        make.top.mas_equalTo(@15);
        make.height.mas_equalTo(@160);
    }];
    
    [self.commitButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(@16);
        make.top.equalTo(self.textView.mas_bottom).with.offset(100);
        make.right.mas_equalTo(@(-16));
        make.height.mas_equalTo(@45);
    }];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTheView)];
    [self.view addGestureRecognizer:tap];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardNotificaion:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardNotificaion:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)tapTheView
{
    [self.view endEditing:YES];
}

- (void)commitFeedback
{
    
}

#pragma mark - Handle Notification
- (void)handleKeyboardNotificaion:(NSNotification *)notification
{
//    if ([notification.name isEqualToString:UIKeyboardWillShowNotification]) {
//        NSDictionary * userInfo = notification.userInfo;
//        NSValue *endFrameValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
//        NSNumber *duration = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
//        CGRect endFrame = endFrameValue.CGRectValue;
//        _originalY = endFrame.size.height;
//        
//        [UIView animateWithDuration:duration.floatValue animations:^{
//            self.view.top -= _originalY;
//        }];
//    }
//    else if ([notification.name isEqualToString:UIKeyboardWillHideNotification]) {
//        NSDictionary * userInfo = notification.userInfo;
//        NSNumber *duration = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
//        [UIView animateWithDuration:duration.floatValue animations:^{
//            self.view.top += _originalY;
//        }];
//    }
}

#pragma mark - UITextViewDelegate
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    if ([textView.text isEqualToString:PLACEHOLDER_STRING]) {
        textView.text = @"";
        textView.textColor = [UIColor colorWithHexRGB:0x606060];
    }
    return YES;
}

- (BOOL)textViewShouldEndEditing:(UITextView *)textView
{
    if (textView.text.length == 0) {
        textView.text = PLACEHOLDER_STRING;
        textView.textColor = PLACEHOLDER_COLOR;
    }
    [self.view endEditing:YES];
    return YES;
}

#pragma mark - Getter Methods
- (UITextView *)textView
{
    if (_textView == nil) {
        _textView = [[UITextView alloc] initWithFrame:CGRectZero];
        _textView.backgroundColor = [UIColor whiteColor];
        _textView.delegate = self;
        _textView.text = PLACEHOLDER_STRING;
        _textView.textColor = PLACEHOLDER_COLOR;
    }
    return _textView;
}

- (UIButton *)commitButton
{
    if (_commitButton == nil) {
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectZero];
        button.backgroundColor = [UIColor colorWithHexRGB:0x0091cd];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(commitFeedback) forControlEvents:UIControlEventTouchUpInside];
        [button setTitle:@"提交" forState:UIControlStateNormal];
        button.layer.masksToBounds = YES;
        button.layer.cornerRadius = 2.0f;
        _commitButton = button;
    }
    return _commitButton;
}

@end
